/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.component;

import java.io.Serializable;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

import org.skatiger.jrove.util.ComponentSupport;

class _ComponentChildrenList extends AbstractList<UIComponent> implements
        Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -1218493490168624598L;
    private UIComponent _component;
    private List<UIComponent> _list = new ArrayList<UIComponent>();

    _ComponentChildrenList(UIComponent component) {
        _component = component;
    }

    @Override
    public void add(int index, UIComponent value) {
        prepare(value);
        _list.add(index, value);
        putComponentToEL(value);
    }

    private void putComponentToEL(UIComponent value) {
        if (value.getAttributes().containsKey(ComponentSupport.NAMED_COMPONENT)) {
            getViewRoot(value).getComponentMap().put(value.getId(), value);
        }
    }

    private void removeComponentFromEL(UIComponent value) {
        if (value.getAttributes().containsKey(ComponentSupport.NAMED_COMPONENT)) {
            getViewRoot(value).getComponentMap().remove(value.getId());
        }
    }

    private UIViewRoot getViewRoot(UIComponent value) {
        return ComponentSupport.getViewRoot(_component.getViewContext(), value);
    }

    @Override
    public boolean add(UIComponent value) {
        prepare(value);
        boolean b = _list.add(value);
        putComponentToEL(value);
        return b;
    }

    @Override
    public UIComponent get(int index) {
        return _list.get(index);
    }

    private void prepare(UIComponent child) {
        UIComponent parent = child.getParent();
        if (parent != null) {
            parent.getChildren().remove(child);
        }
        child.setParent(_component);
    }

    @Override
    public UIComponent remove(int index) {
        UIComponent child = _list.remove(index);
        if (child != null) {
            child.setParent(null);
            removeComponentFromEL(child);
        }
        return child;
    }

    @Override
    public UIComponent set(int index, UIComponent value) {
        prepare(value);
        UIComponent child = _list.set(index, value);
        if (child != null) {
            child.setParent(null);
            removeComponentFromEL(child);
        }
        return child;
    }

    @Override
    public int size() {
        return _list.size();
    }

}
